/**
  *@des： 定义标准的报文apci
  */

#ifndef STD103DEF_H
#define STD103DEF_H

#endif // STD103DEF_H
typedef unsigned short      WORD;
typedef unsigned long       DWORD;

/* 本地字节序 -> 小端字节序, 或小端字节序 -> 本地字节序 */
#define LITTLE_SWAP(x)		(x)
#define LITTLE_SWAP_I32(x)	(x)
#define LITTLE_SWAP_I16(x)	(x)

enum
{
    PACKET_DATA_HDR	= 0xEB90,       // 南瑞继保
    MAX_CTRL_LEN	= 268,
    MAX_S_LEN		= 1028
};

namespace STD103
{
    struct ETH103_SAPDU
    {
        enum
        {
            HEAD_SIZE = 28,
            CFG_SIZE = 20,				// wSourceFactoryId~wReserve1
        };
        WORD  wFirstFlag;            	//0xEB90H
        DWORD dwLength;               	//数据长度， apci的20 + asdu 长度
        WORD  wSecondFlag;           	//0xEB90H
        WORD  wSourceFactoryId;      	//源厂站地址
        WORD  wSourceAddr;           	//源设备地址
        WORD  wDestinationFactoryId; 	//目标厂站地址
        WORD  wDestinationAddr;      	//目标设备地址
        WORD  wDataNumber;          	//数据编号
        WORD  wDeviceType;           	//设备类型
        WORD  wDeviceState;          	//设备网络状态
        WORD  wFirstRouterAddr;      	//传输路径首级路由装置地址
        WORD  wLastRouterAddr;       	//传输路径末级路由装置地址
        WORD  wReserve1;             		//保留字节0xFFFF

        //  ASDU_PACK asdu;
        void Init(WORD SourAddr, WORD DestAddr);
        bool Check();
        DWORD FrmLen();
        inline void toLittle();
    }ATTRIB_PACKED;

    inline void ETH103_SAPDU::Init(WORD SourAddr, WORD DestAddr)
    {
        wFirstFlag				= PACKET_DATA_HDR;
        wSecondFlag				= PACKET_DATA_HDR;
        wSourceFactoryId		= 0x0000;
        wSourceAddr				= SourAddr;
        wDestinationFactoryId	= 0x0000;
        wDestinationAddr		= DestAddr;
        wDeviceType				= 0x002b;//0x0001;
        wDeviceState			= 0x0050;
        //	wDeviceState			= 0x0093;
        wFirstRouterAddr		= 0x0000;
        wLastRouterAddr			= 0x0000;
        wReserve1				= 0xFFFF;
    }

    inline bool ETH103_SAPDU::Check()
    {
        return (wFirstFlag == PACKET_DATA_HDR)
                && (wSecondFlag == PACKET_DATA_HDR);
    }

    void ETH103_SAPDU::toLittle()
    {
        wFirstFlag = LITTLE_SWAP_I16(wFirstFlag);
        dwLength = LITTLE_SWAP_I32(dwLength);
        wSecondFlag = LITTLE_SWAP_I16(wSecondFlag);
        wSourceFactoryId =  LITTLE_SWAP_I16(wSourceFactoryId);
        wSourceAddr	=  LITTLE_SWAP_I16(wSourceAddr);
        wDestinationFactoryId =  LITTLE_SWAP_I16(wDestinationFactoryId);
        wDestinationAddr =  LITTLE_SWAP_I16(wDestinationAddr);
        wDataNumber	= LITTLE_SWAP_I16(wDataNumber);
        wDeviceType	= LITTLE_SWAP_I16(wDeviceType);
        wDeviceState = LITTLE_SWAP_I16(wDeviceState);
        wFirstRouterAddr = LITTLE_SWAP_I16(wFirstRouterAddr);
        wLastRouterAddr	= LITTLE_SWAP_I16(wLastRouterAddr);
        wReserve1 = LITTLE_SWAP_I16(wReserve1);
    }
};
